# 不使用框架和封装技术实现员工模块的增删改查

# 1 导包
import requests

import app1

# 2 调用登陆


response = requests.post(url="http://ihrm-test.itheima.net/api/sys/login",
                         headers={"Content-Type": "application/json"},
                         json={"mobile": "13800000002", "password": "123456"})
print("登录的结果为：", response.json())
# 3 提取登陆返回的令牌
token = "Bearer " + response.json().get("data")
app1.HEADERS["Authorization"] = token  # 把令牌保存到全局变量的请求头当中
# 4 使用提取的令牌，实现查询部门列表接口
response = requests.get(url=app1.BASE_URL + "/api/company/department?page=1&size=2", headers=app1.HEADERS)
print("查询部门列表的结果为：", response.json())


# 5 实现添加部门接口
response = requests.post(url=app1.BASE_URL + "/api/company/department",
                         headers=app1.HEADERS,
                         json={
                             "name":"路哥","code":"001"
                         })
print("添加部门的结果为：", response.json())
# 6 提取部门ID
emp_id = response.json().get("data").get("id")  # 提取部门id
print("保存到全局之前--------id为：", app1.EMP_ID)
app1.EMP_ID = emp_id  # 保存提取到的id到全局变量
print("保存到全局变量id为：", app1.EMP_ID)
# 7 使用提取的部门ID，查询部门
response = requests.get(url=app1.BASE_URL + "/api/company/department" + "/" + app1.EMP_ID, headers=app1.HEADERS)
print("打印查询员工的结果：", response.json())
print("查询员工的URL为：", response.url)

# 8 修改部门
response = requests.put(url=app1.BASE_URL + "/api/company/department" + "/" + app1.EMP_ID, headers=app1.HEADERS,
                        json={"name": "王路"})
print("打印修改员工的结果：", response.json())

# 9 删除部门
response = requests.delete(url=app1.BASE_URL + "/api/company/department" + "/" + app1.EMP_ID, headers=app1.HEADERS)
print("打印查询员工的结果：", response.json())
